#include <REG2051.H> 
#define b1 P3_7
#define b2 P3_3
#define b3 P3_4
#define b4 P3_2
#define mode P3_0 // = 1 -> 9.999 & 0 -> 99.99
unsigned char font1[10]={0x14,0xd7,0x4c,0x45,0x87,0x25,0x24,0x57,0x04,0x05};
int phangiay;
unsigned char giay;
void delayQuet()
{
	unsigned char i,j;
	for(i =0;i<100;i++)
		for(j =0;j<10;j++){};		
} 
void quet1()	// quet 9.999
{							      
	/////////////////////		
	P1=font1[giay%10]&0xfb;
	b1 = 0;
	delayQuet();
	b1=1;	
	///////////////////	
	P1=font1[phangiay/100];
	b2=0;	
	delayQuet();
	b2=1;
	//////////////////	
	P1=font1[(phangiay/10)%10];
	b3=0;
	delayQuet();
	b3=1;
	/////////////////
	P1=font1[phangiay%10];
	b4=0;
	delayQuet();
	b4=1;
	////////////////
}	
void quet2()	// quet 99.99
{							      
	/////////////////////		
	P1=font1[giay/10];
	b1 = 0;
	delayQuet();
	b1=1;	
	///////////////////	
	P1=font1[giay%10]&0xfb;
	b2=0;	
	delayQuet();
	b2=1;
	//////////////////	
	P1=font1[phangiay/100];
	b3=0;
	delayQuet();
	b3=1;
	/////////////////
	P1=font1[(phangiay/10)%10];
	b4=0;
	delayQuet();
	b4=1;
	////////////////
}
void main()
{	
	bit temp;
	P3_5=1;	
	temp = P3_0;
	EA=1;
	ET0=1;
	ET1=1;
	TMOD = 0x21;
	TH1 = TL1 = 0xff;
	TH0=0xFC;
	TL0=0x18;
	TR0=1;
	phangiay=0;
	while(1)
	{			
		if(temp!=P3_0)
		{
			temp=P3_0;
			giay=0;
			phangiay=0;
		}
		if(mode==1) quet1();
		else quet2();
	}
}
void NgatT1(void) interrupt 1
{
	if(mode==1 && giay >=9 && phangiay==999) TR0=0;
	if(mode==0 && giay >=99 && phangiay==999) TR0=0;
	if(TR0==1 && phangiay<=999 && P3_5==1)
		phangiay++;
	if(TR0==1 && phangiay ==1000)
		{
		giay++; 	
		phangiay =0;
		}  	
	TR0=0;
	TH0=0xFC;
	TL0=0x18;
	TR0=1;
}